# encoding = utf-8

from threading import Thread
from time import sleep
'''
# 方法方式（即函数方法）创建线程
def func1 ( name ):
    for i in range(3):
        print(f'thread:{name} :{i}')
        sleep(1)

if __name__ == '__ main__':
    print("主线程,start")

    # 创建线程
    t1 = Thread(target=func1, args=("t1",))
    t2 = Thread(target=func1, args=("t2",))

    # 启动线程
    t1.start()
    t2.start()
    print("主线程,end")
'''

# 类的方法创建线程
class MyThread(Thread):
    def __init__(self,name):
        Thread.__init__(self)
        self.name = name
    def run(self):
        for i in range (3):
            print(f'thread:{self.name} :{i}')
            sleep(1)

if __name__ == '__main__':
    print("创建线程,start")

    t1 = MyThread('t1')
    t2 = MyThread("t2")

    t1.start()
    t2.start()

    # 主线程等待t1，t2结束后，再往下执行
    t1.join()
    t2.join()

    # 设置t1线程为守护线程，生命周期随着主线程一起结束
    t1.daemon = True

    print("主线程,end")